import 'package:film/model/film_model.dart';
import 'package:film/service/api_service.dart';
import 'package:get/get.dart';

class HomeController extends GetxController {
  final DioService apiService = DioService();
  var carouselFilmList = <Film>[].obs;
  var mondayFilmList = <Film>[].obs;
  var tuesdayFilmList = <Film>[].obs;
  var wednesdayFilmList = <Film>[].obs;
  var thursdayFilmList = <Film>[].obs;
  var fridayFilmList = <Film>[].obs;
  var saturdayFilmList = <Film>[].obs;
  var sundayFilmList = <Film>[].obs;
  var recommendFilmList = <Film>[].obs;
  var isLoading = true.obs;
  var selectedFilters = <String, String>{}.obs;
  final selectedDayIndex = DateTime.now().weekday.obs;

  @override
  void onInit() {
    super.onInit();
    getCarouselFilmList();
    getWeekFilmList();
    getRecommendFilmList();
  }

  Future<void> getCarouselFilmList() async {
    final criteria = SearchCriteria(page: "1", limit: "4", vodLevel: "9");
    final ApiResponse<List<Film>> response = await apiService.searchVideo(criteria);
    if (response.success) {
      carouselFilmList.assignAll(response.data ?? []);
    }
  }

  Future<void> getWeekFilmList() async {
    _getFilmListByWeekday("一");
    _getFilmListByWeekday("二");
    _getFilmListByWeekday("三");
    _getFilmListByWeekday("四");
    _getFilmListByWeekday("五");
    _getFilmListByWeekday("六");
    _getFilmListByWeekday("日");
  }

  Future<void> _getFilmListByWeekday(String weekday) async {
    final criteria = SearchCriteria(page: "1", limit: "3", vodWeekday: weekday);
    final response = await apiService.searchVideo(criteria);
    switch (weekday) {
      case "一":
        mondayFilmList.assignAll(response.data ?? []);
        break;
      case "二":
        tuesdayFilmList.assignAll(response.data ?? []);
        break;
      case "三":
        wednesdayFilmList.assignAll(response.data ?? []);
        break;
      case "四":
        thursdayFilmList.assignAll(response.data ?? []);
        break;
      case "五":
        fridayFilmList.assignAll(response.data ?? []);
        break;
      case "六":
        saturdayFilmList.assignAll(response.data ?? []);
        break;
      case "日":
        sundayFilmList.assignAll(response.data ?? []);
        break;
    }
  }

  Future<void> getRecommendFilmList() async {
    final criteria = SearchCriteria(page: "1", limit: "8", vodLevel: "6");
    final ApiResponse<List<Film>> response = await apiService.searchVideo(criteria);
    if (response.success) {
      recommendFilmList.assignAll(response.data ?? []);
    }
  }
}
